﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;



namespace BskLeague.Controllers
{ 
    public class SchedulePrinterController : Controller
    {
        private BskLeagueDB db = new BskLeagueDB();
        private static List<Match> MatchesToPrint = new List<Match>();
        //
        // GET: /SchedulePrinter/

        public ViewResult Index()
        {
            MatchesToPrint.Clear();
            var sortedMatches =
                            (from m in db.Matchs
                             //    where m.Season.Status.Status=="Active"
                             orderby m.Date
                             select m).ToList();

            List<SelectListItem> itemsForDropDown = sortedMatches.Select(p => p.Round).Distinct().OrderBy(p => p).Select(c => new SelectListItem
            {
                Value = c.ToString(),
                Text = c.ToString()
            }).ToList();
            itemsForDropDown.Insert(0, new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });


            ViewBag.itemsForDropDown = itemsForDropDown.ToList();

            var allSeasons = db.Seasons.OrderBy(p => p.SeasonId).ToList();
            List<SelectListItem> seasonsForDropDown = allSeasons.Select(c => new SelectListItem
            {
                Value = c.SeasonId.ToString(),
                Text = c.SeasonName.ToString()
            }).ToList();

            seasonsForDropDown.Insert(0, new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });
            ViewBag.seasonsForDropDown = seasonsForDropDown.ToList();

            List<SelectListItem> gamesTypes = new List<SelectListItem>();
            gamesTypes.Add(new SelectListItem { Value = "All", Text = "Wszystkie", Selected = true });
            gamesTypes.Add(new SelectListItem { Value = "Played", Text = "Rozegrane" });
            gamesTypes.Add(new SelectListItem { Value = "ToPlay", Text = "Do rozegrania" });
            ViewBag.gamesTypes = gamesTypes;

            /////// najblizsze mecze !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




            var stadiums = db.Stadiums.ToList();
            List<SelectListItem> stadiumsForDropDown = stadiums.Select(c => new SelectListItem
            {
                Value = c.stadiumId.ToString(),
                Text = c.stadiumName.ToString()
            }).ToList();

            stadiumsForDropDown.Insert(0, new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });
            ViewBag.stadiumsForDropDown = stadiumsForDropDown.ToList();


     
            return View(sortedMatches);

        }

        [HttpPost]
        public ActionResult Index(List<bool> Selectors, List<int> Matches)
        {
            List<bool> Converted=new List<bool>();
            for (int i = 0; i < Selectors.Count; i++)
            {
                if (Selectors[i] == true && Selectors[i + 1] == false)
                {
                    Converted.Add(true);
                    i++;
                }
                else
                {
                    Converted.Add(false);
                }
            }

            for (int i = 0; i < Converted.Count; i++)
            {
                if (Converted[i])
                {
                    var ma = db.Matchs.Include("Season").Include("Team1").Include("Team2").Include("Place").ToList();
                    var match = (from p in ma
                                 where Matches[i] == p.MatchId
                                 select p).First();
                    MatchesToPrint.Add(match);

                }
            }

            return RedirectToAction("Texts");
        }





        [HttpPost]
        public ActionResult Filter(int season, int round, int stadium, String gamesTypes)
        {
            var matchesSel = db.Matchs.ToList();
            if (-1 != season)
            {
                matchesSel = matchesSel.Where(s => s.Season.SeasonId == season).ToList();
            }


            if (-1 != round)
            {
                matchesSel = matchesSel.Where(s => s.Round == round).ToList();
            }

            if (-1 != stadium)
            {
                matchesSel = matchesSel.Where(s => s.Place.stadiumId == stadium).ToList();
            }

            if (gamesTypes.Equals("Played"))
            {
                matchesSel = matchesSel.Where(s => s.Confirmed1 == true && s.Confirmed2 == true).ToList();
            }
            if (gamesTypes.Equals("ToPlay"))
            {
                matchesSel = matchesSel.Where(s => s.Confirmed1 == false || s.Confirmed2 == false).ToList();
            }
            matchesSel = matchesSel.OrderBy(a => a.Date).ToList();
            return PartialView("ListMatchesPartialCheck", matchesSel);
        }


        public ActionResult Texts()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Texts(string Filename, string PlaceAndDate, string Address, string Main, string Introduction, string Ending, string Sign)
        {

            string path = Server.MapPath("/PDFs/");
            PdfGenerator.GenerateSchedule(Filename, path, PlaceAndDate, Address, Main, Introduction, Ending, Sign,MatchesToPrint);
            return RedirectToAction("Done") ;
        }

        public ActionResult Done()
        {
            MatchesToPrint.Clear();
            return View();
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}